package cn.xfdzx.android.apps.shop.activity.ruzhu;


import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AgreementBeen;
import cn.xfdzx.android.apps.shop.contract.AgreementContract;
import cn.xfdzx.android.apps.shop.presenter.AgreementPresenter;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import cn.xfdzx.android.apps.shop.view.CustomScrollview;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

public class AgreementActivity extends BaseMvpActivity<AgreementPresenter> implements AgreementContract.View {

    @BindView(R.id.ll_state)
    LinearLayout llState;

    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.tv_yi_du)
    TextView tvYiDu;
    @BindView(R.id.sv_view)
    CustomScrollview svView;

    private Context mContext;
    private RelativeLayout.LayoutParams item_bar;
    private String type = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_agreement;
    }

    @Override
    public void initView() {
        mContext = AgreementActivity.this;
        initHigh();
        mPresenter = new AgreementPresenter();
        mPresenter.attachView(this);
        type = getIntent().getStringExtra("type");
        if (type.equals("0")) {
            mPresenter.agreement("1");
        } else {
            mPresenter.agreement("2");
        }

    }

    @Override
    public void showLoading() {

    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    public void onAgreementSuccess(AgreementBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            tvAgreement.setText(bean.getData());
            svView.setScanScrollChangedListener(new CustomScrollview.ScrollViewListener() {
                @Override
                public void onScrolledToBottom() {
                    if (type.equals("0")) {
                        tvYiDu.setBackgroundResource(R.drawable.dfb9129_bg_r8);
                    } else {
                        tvYiDu.setBackgroundResource(R.drawable.d02c557_bg_r8);
                    }
                    tvYiDu.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (Utils.isFastClick()) {
                                Intent intent = new Intent(mContext, InformationActivity.class);
                                intent.putExtra("type", type);
                                startActivity(intent);
                                finish();
                            }
                        }
                    });
                }

                @Override
                public void onScrolledToTop() {

                }
            });

        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(mContext, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }
}
